﻿// -------------------------------------------------------------------------
//    @FileName         :    NFCityNeighborGrid.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-17
//    @Email			:    445267987@qq.com
//    @Module           :    NFCityNeighborGrid
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFCityNeighborGrid.h"

NFCityNeighborGrid::NFCityNeighborGrid()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFCityNeighborGrid::~NFCityNeighborGrid()
{
}

int NFCityNeighborGrid::CreateInit()
{
    return 0;
}

int NFCityNeighborGrid::ResumeInit()
{
    return 0;
}

int NFCityNeighborGrid::AddGrid(int iGridId)
{
    CHECK_EXPR(!m_aiGridID.full(), -1, "neighbor grids full");
    m_aiGridID.push_back(iGridId);
    return 0;
}

int NFCityNeighborGrid::GetGridObjID(int iIdx) const
{
    if (iIdx >= 0 && iIdx < (int)m_aiGridID.size())
    {
        return m_aiGridID[iIdx];
    }
    return INVALID_ID;
}

int NFCityNeighborGrid::IntersectGrids(NFCityNeighborGrid& otherGrids, NFCityNeighborGrid& gridsOriginal, NFCityNeighborGrid& gridsOther)
{
    for (int i = 0; i < (int)otherGrids.m_aiGridID.size(); i++)
    {
        if (!IsInNeighbor(otherGrids.m_aiGridID[i]))
        {
            gridsOther.AddGrid(otherGrids.m_aiGridID[i]);
        }
    }

    for (int i = 0; i < (int)m_aiGridID.size(); i++)
    {
        if (!otherGrids.IsInNeighbor(m_aiGridID[i]))
        {
            gridsOriginal.AddGrid(m_aiGridID[i]);
        }
    }

    return 0;
}

int NFCityNeighborGrid::IterateGrids(NFICityWorldObjectFunc* pFunc, ECityWorldListType eListType)
{
    CHECK_NULL(0, pFunc);

    NFCityMapGrid* pMapGrid = nullptr;
    for (int i = 0; i < (int)m_aiGridID.size(); ++i)
    {
        pMapGrid = NFCityMapGrid::GetObjByGlobalId(m_aiGridID[i]);
        if (pMapGrid)
        {
            pMapGrid->IteratorWorldObject(pFunc, eListType);
        }
    }

    return 0;
}

bool NFCityNeighborGrid::IsInNeighbor(int iGridID)
{
    for (int i = 0; i < (int)m_aiGridID.size(); i++)
    {
        if (m_aiGridID[i] == iGridID)
        {
            return true;
        }
    }
    return false;
}
